;(function() {
  
  var sHeight = getViewportSize().height, // 视口的高度
      wHeight = getScrollSize().height,   // 文档的高度
      playing = false, // 默认不播放
      t = null; // 定时器

  var AutoReader = function(opt) {
    this.playBtn = opt.oPlayer;
    this.sTopBtn = opt.sTopBtn;

    var _self = this;

    addEvent(window, 'scroll', function() {
      _self.sTopBtnShow();
    });

    addEvent(this.sTopBtn, 'click', function() {
      window.scrollTo(0, 0);
    });

    addEvent(this.playBtn, 'click', function() {
      _self.setAutoPlayer();
    });
  }

  AutoReader.prototype = {
    setAutoPlayer: function() {
      // 滚动条的距离
      var sTop = getScrollOffset().top,
          _self = this;

      // 文档的高度 = 视口高度 + 滚动条的距离 =》 滑动到页面底部了
      if(wHeight === sHeight + sTop) return;

      if(!playing) {
        playing = true;

        t = setInterval(function() {
          var sTop = getScrollOffset().top;

          // 滚动到页面底部
          if(wHeight <= sHeight + sTop) {
            clearInterval(t);
            _self.playBtn.innerText = '>';
          }else {
            window.scrollBy(0, 5);
            _self.playBtn.innerText = '| |';
          }
        }, 50);

      }else {
        clearInterval(t);
        this.playBtn.innerText = '>';
        playing = false; // 不播放
      }
    },
    
    sTopBtnShow: function() {
      var sTop = getScrollOffset().top,
          sTopBtn = this.sTopBtn;

      sTopBtn.style.display = sTop ? 'block' : 'none';
    }
  }

  window.AutoReader = AutoReader;
})();